import { request } from '../request';

export function fetchGetCaptcha() {
  return request<Api.Auth.CaptchaInfo>({
    url: '/auth/captcha',
    method: 'get'
  });
}

export function fetchSendEmailCaptcha(emailAccount: string) {
  return request({
    url: '/auth/sendEmailCaptcha',
    method: 'GET',
    params: { emailAccount }
  });
}

/**
 * Login
 *
 * @param userName User name
 * @param password Password
 */
export function fetchLogin(userName: string, password: string) {
  return request<Api.Auth.LoginToken>({
    url: '/auth/login',
    method: 'post',
    data: {
      userName,
      password
    }
  });
}

/**
 * @param userName
 * @param password
 * @param captcha
 * @param captchaKey
 */
// eslint-disable-next-line max-params
export function fetchLoginWithCaptcha(userName: string, password: string, captcha: string, captchaKey: string) {
  return request<Api.Auth.LoginToken>({
    url: '/auth/login',
    method: 'post',
    data: {
      userName,
      password,
      captcha,
      captchaKey
    }
  });
}

export function fetchLoginWithEmail(mailAccount: string, verifyCode: string) {
  return request<Api.Auth.LoginToken>({
    url: '/auth/login/email',
    method: 'post',
    data: {
      mailAccount,
      verifyCode
    }
  });
}

/** Get user info */
export function fetchGetUserInfo(token: string) {
  return request<Api.Auth.UserInfo>({
    url: `/auth/getUserInfo?token=${token}`
  });
}

/** logout */
export function fetchLogout() {
  return request({
    url: '/auth/logout'
  });
}

/**
 * Refresh token
 *
 * @param refreshToken Refresh token
 */
export function fetchRefreshToken(refreshToken: string) {
  return request<Api.Auth.LoginToken>({
    url: '/auth/refreshToken',
    method: 'post',
    data: {
      refreshToken
    }
  });
}

/**
 * return custom backend error
 *
 * @param code error code
 * @param msg error message
 */
export function fetchCustomBackendError(code: string, msg: string) {
  return request({ url: '/auth/error', params: { code, msg } });
}
